package com.study.parse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import com.alibaba.fastjson.JSONObject;
import com.study.common.SpringContextsUtil;
import com.study.model.Student;

/**
 * 订单下发（代理模式）
 *
 * @author Administrator
 */
public class TestParser implements Parser {
    private Logger log = Logger.getLogger(TestParser.class);

    private AmqpTemplate amqpTemplate = (AmqpTemplate) SpringContextsUtil.getBean(RabbitTemplate.class);
    //private static final String TEST_ORDER_JSON = (String) CustomPropertyConfig.getContextProperty("test.order.json");
    

    public String doParse(String str) {
        JSONObject msg = null;
        try {
            msg = JSONObject.parseObject(str);
        } catch (Exception e) {
            e.printStackTrace();
            log.debug("消息解析错误：", e);
        }
        if (msg == null)
            return str;


        String opTypeStr = msg.get("op_type")==null?"":msg.getString("op_type"); // 操作类型
        if(!StringUtils.isBlank(opTypeStr)){
            log.info("操作类型：" + msg);
            return str;
        }
        
        
        String shopId ="";
         MessageProperties messageProperties = new MessageProperties();
         messageProperties.setHeader("op_type", 500);
         messageProperties.setHeader("shop_id", shopId);
         messageProperties.setHeader("cloud_order_id", "10000");
         Message message = new Message(str.getBytes(), messageProperties);
         log.info("测试订单数据下发....");
         amqpTemplate.convertAndSend("testDown", "testDownQueue", message);
         log.info("测试订单数据下发成功!");
         Student student = new Student();
         student.set("name", str).save();
        
        return "";
    }

}


